<template>
  <view class="wrapper">
    <!-- 商品图片 -->
    <swiper
      class="pics"
      indicator-dots
      indicator-color="rgba(255, 255, 255, 0.6)"
      indicator-active-color="#fff"
    >
      <!-- 商品图片列表 -->
      <swiper-item v-for="item in detail.pics" :key="item.pics_id">
        <image :src="item.pics_big_url" />
      </swiper-item>
    </swiper>
    <!-- 基本信息 -->
    <view class="meta">
      <view class="price">￥{{ detail.goods_price }}</view>
      <view class="name">{{ detail.goods_name }}</view>
      <view class="shipment">快递: 免运费</view>
      <text class="collect icon-star">收藏</text>
    </view>
    <!-- 商品详情
    后台返回的是dom字符串=》detail.goods_introduce
    如何解析？
    1. vue=> v-html="detail.goods_introduce" => dom.innerHTML
    2. 小程序(没有dom)=》借助rich-text解析
     -->
    <!-- <view class="detail" v-html="detail.goods_introduce"> -->
    <view class="detail">
      <rich-text :nodes="detail.goods_introduce"></rich-text>
    </view>
    <!-- 操作 -->
    <view class="action">
      <!-- 显示购物车中商品数据 -->
      <text v-if="carts.length > 0" class="cartCount">{{ carts.length }}</text>
      <button open-type="contact" class="icon-handset">联系客服</button>
      <text class="cart icon-cart" @click="goCart">购物车</text>
      <text @click="addCart" class="add">加入购物车</text>
      <text class="buy" @click="createOrder">立即购买</text>
    </view>
  </view>
</template>

<script>
import { CART_KEY } from '@/utils/const.js'
export default {
  data () {
    return {
      // 商品详情数据
      detail: {},
      // 购物车列表数据
      carts: uni.getStorageSync(CART_KEY) || []
    }
  },
  onLoad ({ id }) {
    this.getDetail(id)
  },
  methods: {
    // 加入购物车
    addCart () {
      /**
       * 加入商品到购物车思路：
       * 考虑之前这个商品有没有加入过购物车（排重）？
       * 1. 没有加入过 =》执行push
       * 2. 加入过 =》数量加一
       */
      // 1. find方法返回值：加入过 =》good已经加入的商品  2. 没有加入过返回undefined
      const good = this.carts.find(item => item.goods_id === this.detail.goods_id)
      if (!good) {
        // 没有加入过
        // 后台需要的加入购物车商品的属性：
        // goods_id,  goods_name,  goods_price,  goods_small_logo,   goods_count（数量），goods_checked（是否被选中）
        // 1. 从当前商品获取
        const { goods_id, goods_name, goods_price, goods_small_logo } = this.detail
        // 2. 把当前商品加入cart数组中
        this.carts.push({
          goods_id, goods_name, goods_price, goods_small_logo,
          goods_count: 1, // 第一次加入购物车，数量为一
          goods_checked: false // 加入购物车之后商品选中状态
        })

      } else {
        // 加入过 
        good.goods_count++
      }

      // 存储到本地
      uni.setStorageSync(CART_KEY, this.carts)
      uni.showToast({
        title: '加入成功！',
        duration: 2000
      });
    },
    // 获取商品详情数据
    async getDetail (id) {
      const { data } = await this.request({
        url: '/api/public/v1/goods/detail',
        data: {
          goods_id: id
        }
      })
      console.log('商品详情：', data)
      this.detail = data
    },
    goCart () {
      uni.switchTab({
        url: "/pages/cart/index"
      });
    },
    createOrder () {
      uni.navigateTo({
        url: "/pages/order/index"
      });
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  margin-bottom: 100rpx;
  background-color: #f4f4f4;
}

.pics {
  height: 640rpx;
}

.meta {
  height: 250rpx;
  line-height: 1;
  padding: 30rpx 180rpx 30rpx 20rpx;
  box-sizing: border-box;
  background-color: #fff;
  position: relative;

  .price {
    font-size: 36rpx;
    color: #ea4451;
    margin-bottom: 20rpx;
  }

  .name {
    color: #333;
    line-height: 1.4;
    font-size: 33rpx;

    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }

  .shipment {
    font-size: 27rpx;
    color: #999;
    position: absolute;
    bottom: 30rpx;
  }

  .collect {
    width: 140rpx;
    height: 88rpx;
    text-align: center;
    box-sizing: border-box;
    border-left: 1rpx solid #ddd;
    font-size: 24rpx;
    color: #999;

    position: absolute;
    right: 10rpx;
    top: 91rpx;
  }

  [class*="icon-"]::before {
    display: block;
    font-size: 45rpx;
    margin-bottom: 10rpx;
  }
}

.detail image {
  width: 100%;
  height: 480rpx;
  margin-top: 20rpx;
}

.action {
  width: 100%;
  height: 98rpx;
  background-color: #fff;

  position: fixed;
  left: 0;
  bottom: 0;

  display: flex;
  align-items: center;

  text {
    display: block;
  }
  // 控制小红点位置
  .cartCount {
    position: absolute;
    top: -15rpx;
    left: 250rpx;
    width: 50rpx;
    height: 50rpx;
    line-height: 50rpx;
    border-radius: 50rpx;
    text-align: center;
    background-color: #ea4451;
    color: #fff;
    font-size: 24rpx;
  }

  .add,
  .buy {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 210rpx;
    text-align: center;
    font-size: 27rpx;
    color: #fff;
  }

  .add {
    background-color: #f4b73f;
  }

  .buy {
    background-color: #ea4451;
  }

  button {
    padding: 0;
    border-radius: 0;
    background-color: #fff;

    &::after {
      border: none;
    }
  }

  button,
  .cart {
    flex: 1;
    text-align: center;
    color: #989898;
    font-size: 24rpx;
    box-sizing: border-box;
  }

  [class*="icon"]::before {
    display: block;
    font-size: 45rpx;
    margin-bottom: 2rpx;
  }
}
</style>
